﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.Mtbl;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.DALFactory;
using System.Linq.Dynamic;
namespace HKEA.CMS.Core.DAL.Mtbl
{
    public class mtblSourceDAL:ImtblSource
    {
        HKEA_CMSEntities model;
        mtblSource source;

        public mtblSourceDAL()
        {
            model = DataAccess.GetModelInstance();
        }

        public static mtblSourceDAL GetInstance()
        {
            return new mtblSourceDAL();
        }

        public int InsertENT(mtblSource ent)
        {
            ent.CreationDate = DateTime.Now;
            ent.LastModifyDate = DateTime.Now;
            model.mtblSources.AddObject(ent);
            return model.SaveChanges();
        }

        public List<mtblSource> GetListByENT(mtblSource ent, PageParam param, string strPropertyName,string strOrderBy)
        {
            var result = model.mtblSources.Where(a => a.SourceUID > 0);
            if (ent.SourceUID > 0)
                result = result.Where(a => a.SourceUID == ent.SourceUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            param.Count = result.Count();
            result = result.OrderBy(strPropertyName + " " + strOrderBy).Skip((param.CurrentPage - 1) * param.PageSize).Take(param.PageSize);
            return result.ToList();
        }
        public List<mtblSource> GetListByENT(mtblSource ent)
        {
            var result = model.mtblSources.Where(a => a.SourceUID > 0);
            if (ent.SourceUID > 0)
                result = result.Where(a => a.SourceUID == ent.SourceUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.ToList();
        }
        public int GetCountByENT(mtblSource ent)
        {
            var result = model.mtblSources.Where(a => a.SourceUID > 0);
            if (ent.SourceUID > 0)
                result = result.Where(a => a.SourceUID == ent.SourceUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.Count();
        }

        public int UpdateENT(mtblSource ent)
        {
            source = model.mtblSources.Where(a=>a.SourceUID==ent.SourceUID).FirstOrDefault();
            if (source != null)
            {
                source.ActiviteUID = ent.ActiviteUID;
                source.Deleted = ent.Deleted;
                source.LastModifyBy = ent.LastModifyBy;
                source.LastModifyDate = DateTime.Now;
                source.Source_Chi = ent.Source_Chi;
                source.Source_Eng = ent.Source_Eng;
                source.Source_Date_From = ent.Source_Date_From;
                source.Source_Date_To = ent.Source_Date_To;
            }
            return model.SaveChanges();
        }

        public int DeleteENT(mtblSource ent)
        {
            var temp = model.mtblSources.Where(a => a.SourceUID > 0);
            if (ent.SourceUID > 0)
                temp = temp.Where(a=>a.SourceUID==ent.SourceUID);
            var list = temp.ToList();
            for (int i = 0; i < list.Count(); i++)
            {
                model.mtblSources.DeleteObject(list[i]);
            }
            return model.SaveChanges();
        }
    }
}
